﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class ViewMyCart : System.Web.UI.Page
{
    ShopDataContext ds = new ShopDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["MyCart"] != null)
            {
                Basket b = Session["MyCart"] as Basket;
                ListView1.DataSource = b.Items;
                ListView1.DataBind();
            }
        }
    }
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("add"))
        {
            Label l = e.Item.Controls[0].FindControl("lblCode") as Label;
            TextBox t = e.Item.Controls[0].FindControl("txtQuantity") as TextBox;
            Basket b = Session["MyCart"] as Basket;
            b.Items.FirstOrDefault(i => i.ProductCode.Equals(l.Text)).Quantity = Convert.ToInt32(t.Text);
            Session["MyCart"] = b;
        }
        if (e.CommandName.Equals("123"))
        {
            Label l = e.Item.Controls[0].FindControl("lblCode") as Label;
            Basket b1 = Session["MyCart"] as Basket;
            b1.Remove(b1.Items.FirstOrDefault(i => i.ProductCode.Equals(l.Text)));
            ListView1.DataSource = b1.Items;
            ListView1.DataBind();
            Session["MyCart"] = b1;
        }
    }
    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        Label l = e.Item.FindControl("lblName") as Label;
        l.Text = ds.Products.FirstOrDefault(p => p.pProductCode == l.Text).pName;

    }
}